package com.test.timmy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.res.AssetManager;

/**
 * My singleton.
 *
 */
public class Controller
{
	private static final String TAG_PEOPLE = "people";
	private static final String TAG_LIFE_EVENTS = "life_events";
	
	private static final String PATH_TO_ONLINE_JSON = 
			"http://api.timmystudios.com/api/office.php";
	//reason for using always online resources(json, photos) was to reduce time spent on issue 
	private static final String PATH_TO_LOCAL_JSON = 
			"all";
	
	private Map<String, Person> people = new HashMap<String, Person>();
	private Map<String, LifeEvent> lifeEvents = new HashMap<String, LifeEvent>();
	
	private static volatile Controller instance = null;
	
	private Controller()
	{
		;
	}
	
	private Controller(AssetManager assets)
	{
		//yes, the application will "freeze" until all the images are downloaded
		BufferedReader isReader = null;
		JSONObject parser = null;
	    try {
	    	InputStream is = (InputStream)new URL(PATH_TO_ONLINE_JSON).getContent();
	    	isReader = new BufferedReader(new InputStreamReader(is));
	    	//isReader = new BufferedReader(new InputStreamReader(assets.open(PATH_TO_LOCAL_JSON)));
	        StringBuilder sb = new StringBuilder();
	        String line;
	        while ((line = isReader.readLine()) != null) {
	        	sb.append(line);
	        }
	        isReader.close();
	        String jsonString = sb.toString();
	        parser = new JSONObject(jsonString);

            JSONArray jsonArray = new JSONArray(parser.getString(TAG_PEOPLE));
            for (int i = 0; i < jsonArray.length(); i++) {
              JSONObject jsonObject = jsonArray.getJSONObject(i);
              Person person = new Person(jsonObject);
              //unique id will be name_department; let's hope no collisions
              people.put(person.getUniqueID(), person);
            }

            jsonArray = new JSONArray(parser.getString(TAG_LIFE_EVENTS));
            for (int i = 0; i < jsonArray.length(); i++) {
              JSONObject jsonObject = jsonArray.getJSONObject(i);
              LifeEvent lifeEvent = new LifeEvent(jsonObject);
              lifeEvents.put(lifeEvent.getLabel(), lifeEvent);
            }
	    } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	    	try {
	    		if (isReader != null)
	    		{
	    			isReader.close();
	    		}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	}

	public static Controller getInstance()
	{
		//let it be null if init(AssetManager assets) wasn't called
		//should synchronization be an issue?
		return instance;
	}
	
	public static Controller init(AssetManager assets)
	{
		instance = new Controller(assets);
		return instance;
	}
	
	public Map<String, Person> getPeople()
	{
		return people;
	}
	
	public Map<String, LifeEvent> getLifeEvents()
	{
		return lifeEvents;
	}
}
